//
//  DZBuyVipController.m
//  dzyc
//
//  Created by 蓝鳍互娱 on 2024/6/14.
//

#import "DZBuyVipController.h"
#import "DZApplePayHelper.h"
#import "DZGuidePage.h"
@interface DZBuyVipController ()
@property (weak, nonatomic) IBOutlet UILabel *buyVipPriceLab;

@end

@implementation DZBuyVipController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    [self updataVipPrice];
}
- (void)updataVipPrice{
    if([DZGlobleData defaultManager].globleVipPrice){
        NSString * attString = [NSString stringWithFormat:@"订阅费用会在用户确认购买时从 iTunes 账户扣除，可自动续订续费￥%@/周，免费享受后续功能迭代，无需额外付费",[DZGlobleData defaultManager].globleVipPrice];
        NSString *changeString = [NSString stringWithFormat:@"续费￥%@/周",[DZGlobleData defaultManager].globleVipPrice];
        NSRange changeRange = [attString rangeOfString:changeString];
        
        NSMutableAttributedString *attMutStr = [[NSMutableAttributedString alloc] initWithString:attString];
        [attMutStr addAttribute:NSForegroundColorAttributeName value:DZHex(@"#FFFFFF") range:NSMakeRange(0,attString.length)];
        [attMutStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17 weight:UIFontWeightRegular] range:NSMakeRange(0,attString.length)];
        [attMutStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20 weight:UIFontWeightBold] range:changeRange];
        self.buyVipPriceLab.attributedText = attMutStr;
    }
}
- (IBAction)closeNav:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:^{}];
}
- (IBAction)buyVip:(UIButton *)sender {
    DZWeakSelf
    [[DZApplePayHelper defaultManager] beginApplePay:DZProductId];
    [DZApplePayHelper defaultManager].ApplePayBuyVipBlock = ^{//购买成功
        [NSString saveBuyTime];
        if(weakSelf.ApplePayBuyVipBlock){
            [DZGlobleData getAppMaiDian];//埋点
            weakSelf.ApplePayBuyVipBlock();
            [weakSelf dismissViewControllerAnimated:YES completion:^{}];
        }
    };
}
- (IBAction)repareVip:(UIButton *)sender {
    DZWeakSelf
    [[DZApplePayHelper defaultManager] beginApplePay:DZProductId];
    [DZApplePayHelper defaultManager].ApplePayRepeatBuyVipBlock = ^{//恢复购买成功
        [NSString saveBuyTime];
        if(weakSelf.ApplePayRepeatBuyVipBlock){
            weakSelf.ApplePayRepeatBuyVipBlock();
            [weakSelf dismissViewControllerAnimated:YES completion:^{}];
        }
    };
}
- (IBAction)userAgreement:(UIButton *)sender {
    DZWebViewController * webViewCtrl = [[DZWebViewController alloc] init];
    webViewCtrl.webUrl = DZUserAgreement;
//    UINavigationController *navWebCtrl = [[UINavigationController alloc]initWithRootViewController:webViewCtrl];
    [self.navigationController pushViewController:webViewCtrl animated:YES];
}
- (IBAction)privaAgreement:(UIButton *)sender {
    DZWebViewController * webViewCtrl = [[DZWebViewController alloc] init];
    webViewCtrl.webUrl = DZPriveAgreement;
    [self.navigationController pushViewController:webViewCtrl animated:YES];
}
@end
